<?php
class ControllerSoftCategory extends Controller
{
	function index()
	{	
		$this->data['catagory'] = $this->getMenu("category","soft/product");
		
		$this->data['service'] = $this->getMenu("service","soft/service");
		$this->id='category';
		$this->template='common/category.tpl';
		$this->render();
	}
	
	public function getMenu($parentid,$module)
	{
		$this->load->model("core/sitemap");
		$siteid = $this->user->getSiteId();
		$str = "";

		$sitemaps = $this->model_core_sitemap->getListByParent($parentid, $siteid, "Active");
		
		foreach($sitemaps as $item)
		{
			$childs = $this->model_core_sitemap->getListByParent($item['sitemapid'], $siteid, "Active");
			
			$link = "<a class='left'>".$item['sitemapname']."</a>";
			if($item['modulepath'] != "")
				$link='<a class="left" href="?route='.$module.'&sitemapid='.$item['sitemapid'].'" title="[Detail]">'.str_replace("<br />","",html_entity_decode($item['sitemapname'])).'</a>';
			
			$str .= "<li>";
			$str .= "<div class='collape'>";
			$str .= $link;
			
			if(count($childs) > 0)
			{
				$str .= "<span class='collape right'>[+]</span>";
				$str .= '<div class="clearer">&nbsp;</div>';
				$str .= "</div>";
				
				$str .= "<ul>";
				$str .= $this->getMenu($item['sitemapid'],$module);
				$str .= "</ul>";
			}
			else
			{
				$str .= '<div class="clearer">^&nbsp;</div>';
				$str .= "</div>";
				
			}
			$str .= "</li>";
		}
		$this->data['output'] = $str;
		$this->template='common/output.tpl';
		$this->render();
		return $str;
		
	}
	
	function management()
	{
		$this->getList("category");
		$this->id='content';
		$this->template='soft/category.tpl';
		$this->layout='layout/center';
		$this->render();
	}
	
	function managementservice()
	{
		$this->getList("service");
		$this->id='content';
		$this->template='soft/service.tpl';
		$this->layout='layout/center';
		$this->render();
	}
	
	public function save()
	{
		$this->load->model("core/sitemap");
		$categoryid = $this->request->post['CatelogyID'];
		$categoryname = $this->request->post['CategoryName'];
		$categoryparent = $this->request->post['CategoryParent'];
		$position = $this->request->post['Position'];
		$data['sitemapid'] = $categoryid;
		$data['siteid'] = $this->user->getSiteId();
		$data['position'] = $position;
		$data['sitemapparent'] = $categoryparent;
		$data['sitemapname'] = $categoryname;
		$data['othername'] = $categoryname;
		$data['moduleid'] = "module/product";
		
		$data['status'] = "Active";
		$arr = $this->model_core_sitemap->getItem($categoryid,$this->user->getSiteId());
		if(count($arr) == 0)
		{
			$data['position'] = $this->model_core_sitemap->nextPosition("category");
			$this->model_core_sitemap->insertSiteMap($data);
		}
		else
		{	
			$this->model_core_sitemap->updateSiteMap($data);
		}
		$this->data['output'] = 'true';
		$this->id='category';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function getList($root)
	{
		$this->load->model("core/sitemap");
		$this->data['category'] = array();
		$this->model_core_sitemap->getTree($root,$this->data['category']);
		foreach($this->data['category'] as $key => $item)
		{
			$deep = $this->model_core_sitemap->getDeep($item['sitemapid'],$this->user->getSiteId());
			$prefix = $this->string->getPrefix("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",$deep-1);
			$this->data['category'][$key]['sitemapname'] = $prefix.$item['sitemapname'];
		}
		
	}
	
	public function getCategory()
	{
		$this->load->model("core/sitemap");
		$categoryid = $this->request->get['categoryid'];
		$category=$this->model_core_sitemap->getItem($categoryid,$this->user->getSiteId());
		$category['sitemapname'] = html_entity_decode($category['sitemapname']);
		$this->data['output'] = json_encode(array('category' => $category));
		$this->id='post';
		$this->template="common/output.tpl";
		$this->render();
	}
	
	public function removeCategory()
	{
		$this->load->model("core/sitemap");
		$this->load->model("core/media");
		$categoryid = $this->request->get['categoryid'];
		//Kiem tra toan ven du lieu
		$where = " AND refersitemap = '".$categoryid."'";
		$list = $this->model_core_media->getList($where);
		if(count($list)==0)
		{
			$this->model_core_sitemap->deleteSiteMap($categoryid, $this->user->getSiteId());
			$this->data['output'] = "true";
		}
		else
		{
			$this->data['output'] = "false";
		}
		
		$this->id='post';
		$this->template="common/output.tpl";
		$this->render();
	}
}
?>